package com.hll.basic;

import com.hll.basic.pojo.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** Date:2021/7/9 Author:ybc Description: */
@Controller
@RequestMapping("/resp")
// @RestController 加上这个所有方法默认 @ResponseBody
public class ResponseController {

  // requestBody:username=jack&password=12345
  @RequestMapping("/testRequestBody")
  public String testRequestBody(@RequestBody String requestBody) {
    System.out.println("requestBody:" + requestBody);
    return "success";
  }
  // 请求头：[host:"localhost:8080", connection:"keep-alive", content-length:"28",
  // cache-control:"max-age=0", sec-ch-ua:"" Not A;Brand";v="99", "Chromium";v="101", "Google
  // Chrome";v="101"", sec-ch-ua-mobile:"?0", sec-ch-ua-platform:""Windows"",
  // upgrade-insecure-requests:"1", origin:"http://localhost:8080", user-agent:"Mozilla/5.0 (Windows
  // NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41
  // Safari/537.36",
  // accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", sec-fetch-site:"same-origin", sec-fetch-mode:"navigate", sec-fetch-user:"?1", sec-fetch-dest:"document", referer:"http://localhost:8080/mvc/axios", accept-encoding:"gzip, deflate, br", accept-language:"zh-CN,zh;q=0.9", cookie:"JSESSIONID=A4128CCFDCB7B4A9237BE6D79220DECD; Webstorm-c3cf2707=33680171-200a-47d0-91e7-6aaf662590ea; _dd_s=logs=1&id=c35e7be9-9140-4fe8-a7ff-1c7d27c03b15&created=1652347353012&expire=1652349581965", Content-Type:"application/x-www-form-urlencoded;charset=UTF-8"]
  // 请求体：username=jack&password=12345
  @RequestMapping("/testRequestEntity")
  public String testRequestEntity(RequestEntity<String> requestEntity) {
    // 当前requestEnity表示整个请求报文的信息
    System.out.println("请求头：" + requestEntity.getHeaders());
    System.out.println("请求体：" + requestEntity.getBody());
    return "success";
  }

  @RequestMapping("/testResponse")
  public void testResponse(HttpServletResponse response) throws IOException {
    response.getWriter().print("hello,response");
  }

  @RequestMapping(value = "/testResponseBody", produces = "text/html;charset=UTF-8")
  @ResponseBody
  public String testResponseBody() {
    return "成功";
  }

  // No converter found for return value of type: class com.hll.basic.pojo.User
  // 上述错误解决方法1：给User类每个属性get set方法
  // 2: <!--<dependency>
  //            <groupId>com.fasterxml.jackson.core</groupId>
  //            <artifactId>jackson-databind</artifactId>
  //            <version>2.12.1</version>
  //        </dependency>-->
  @RequestMapping("/testResponseUser")
  @ResponseBody
  public User testResponseUser() {
    return new User(0, "jack", "1234", 18, "男", "31@qq.com");
  }

  @RequestMapping("/testAxios")
  @ResponseBody
  public String testAxios(String username, String password) {
    System.out.println(username + "," + password);
    return "hello,axios";
  }
}
